﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// Default implementation of CounterSuperRows is just a list of CounterSuperRow.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class CounterSuperRowsImpl<K,SN,N> : CounterSuperRows<K, SN, N>
    {
        public CounterSuperRowsImpl(Dictionary<K, List<CounterSuperColumn>> thriftRet, Serializer<K> keySerializer,
            Serializer<SN> sNameSerializer, Serializer<N> nameSerializer)
        {
            if (thriftRet == null || keySerializer == null || sNameSerializer == null || nameSerializer == null)
            {
                throw new NullReferenceException("Columns, Key serializer, Super name Serializer and Name serializer cannot be null");
            }
            KeySerializer = keySerializer;
            SuperNameSerializer = sNameSerializer;
            Rows = new Dictionary<K, CounterSuperRow<K, SN, N>>(thriftRet.Count);
            foreach (K key in thriftRet.Keys)
            {
                Rows[key] = new CounterSuperRowImpl<K, SN, N>(key, thriftRet[key], sNameSerializer, nameSerializer);
            }
        }
        protected Dictionary<K, CounterSuperRow<K, SN, N>> Rows
        {
            get;
            set;
        }
        public Serializer<K> KeySerializer
        {
            get;
            protected set;
        }
        public Serializer<SN> SuperNameSerializer
        {
            get;
            set;
        }
        public CounterSuperRow<K, SN, N> GetByKey(K key)
        {
            return Rows[key];
        }
        public int Count
        {
            get
            {
                return Rows.Count;
            }
        }
        public int GetCount()
        {
            return Rows.Count;
        }
        public IEnumerator<CounterSuperRow<K, SN, N>> GetEnumerator()
        {
            return Rows.Values.GetEnumerator();
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        public override string ToString()
        {
            return "SuperRows(" + Rows + ")";
        }
    }
}
